package com.smcdd.cdb.dbo.cdb_dbo.service.presenter;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.app.App;
import com.smcdd.cdb.dbo.cdb_dbo.network.HttpObserver;
import com.smcdd.cdb.dbo.cdb_dbo.network.factory.RetrofitFactory;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.BaseResponseEntity;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.DataManager;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.NetWorkCodeInfo;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.IGetHardware;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BasePresenter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainEncry;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainVersion;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ToastUtil;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 作者：Administrator on 2017/7/13 18:41
 * 邮箱：zhanghuaiha@gmail.com
 */

public class GetHardwarePresenter extends BasePresenter<IGetHardware> {

    private String eid;
    private String versionCode;
    private String udid;

    public GetHardwarePresenter(BaseActivity context) {
        super(context);
    }

    public void loadDate(){
        versionCode = ObtainVersion.getVersionName(mContext);
        eid = App.SP.getString("id", "");
        udid = App.SP.getString("udid", "");
    }

    public void save(String alias){
        //寄回工厂
        String tiem4 = System.currentTimeMillis() + "";
        String encry4 = ObtainEncry.getStart2charging("2", tiem4, versionCode, NetWorkCodeInfo.SEND_FACTORY, udid,alias,eid);
        RetrofitFactory.getmInstanse().mApi.getSend_factory("2", tiem4, versionCode, NetWorkCodeInfo.SEND_FACTORY, udid,alias,eid,encry4)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseResponseEntity<String>>() {
                    @Override
                    public void success(BaseResponseEntity<String> stringBaseResponseEntity) {
                        if (stringBaseResponseEntity.code == 200) {
                            ToastUtil.showToast(mContext.getString(R.string.success_factory));
                            mContext.finish();
                        }else {
                            ToastUtil.showToast(stringBaseResponseEntity.message);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        ToastUtil.showToast(mContext.getString(R.string.error_factory));
                    }
                });

    }
}
